//
//  TableViewCell.m
//  记账本
//
//  Created by lirilu on 15/3/2.
//  Copyright (c) 2015年 glirlu@gmail.com. All rights reserved.
//

#import "TableViewCell.h"

@implementation TableViewCell

+ (BOOL) regestNib:(UITableView*) tabelView Identifier:(NSString *)identifier {
    static UINib *nib ;
    if (nib == nil) {
        UINib *nib = [UINib nibWithNibName:NSStringFromClass(self) bundle:nil];
        [tabelView registerNib:nib forCellReuseIdentifier:identifier];
    }
    return YES;
}

+ (TableViewCell*) getCell:(UITableView*) tableView Account:(Account*)data delegate:(id<TableViewCellDelegate>)delegate{
    static NSString *CellWithIdentifier = @"Cell";
    [TableViewCell regestNib:tableView Identifier:CellWithIdentifier];
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
    if (cell == nil) {
        cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellWithIdentifier];
    }
    cell.accountData = data;
    cell.delegate = delegate;
    [cell.mark setEnabled:NO];
    return  cell;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setAccountData:(Account *)accountData{
    if (_accountData != nil) {
        _accountData = accountData;
    }else{
        _accountData = [[Account alloc]init];
        _accountData = accountData;
    }
    self.payMode.text = _accountData.PayModeStr;
    if (_accountData.Money >= 0) {
        [self.money setTextColor:[UIColor greenColor]];
        self.money.text = [NSString stringWithFormat:@"%.2f",_accountData.Money];
        self.outOrInPicture.image = [UIImage imageNamed:@"gaoshuaifu.png"];
    }else{
        [self.money setTextColor:[UIColor redColor]];
        self.money.text = [NSString stringWithFormat:@"%.2f",-_accountData.Money];
        self.outOrInPicture.image = [UIImage imageNamed:@"diaosi.png"];
    }
    self.date.text = [NSString stringWithFormat:@"%@",_accountData.Dotime];
    self.mark.text = _accountData.Detail;
}

#pragma mark TextFileDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (textField == self.mark) {
        [self.mark resignFirstResponder];
        self.accountData.Detail = self.mark.text;
        //代理传值
        if ([self.delegate respondsToSelector:@selector(accountChange:)]) {//判断是否已经响应了代理方法
            [self.delegate accountChange:self.accountData];
        }
    }
    return YES;
}

@end
